<?php

require_once 'db.class.php';

class Articles extends Db
{
	public $tbname = 'articles';

	public function getCates($type=1)
	{
		$this->sql = "select cates,catename,count(*) as total from ".$this->tbname." where type=".$type." group by cates";
		return $this->dosql('_select');
	}

	public function getArticleByCates($num=10)
	{
		$cates = $this->getCates();
		if($cates):
		foreach($cates as $val)
		{
			$this->sql = "select id,title from ".$this->tbname." where cates=".$val['cates']." order by addtime desc limit 0,".$num;
			$res = $this->dosql('_select');
			if($res):
			$data[] = array(
				'title' => $val['catename'],	
				'list' => $res
			);	
			endif;
		}
		endif;
		return $data;
	}

	public function getArticleById($id)
	{
		$this->sql = "select id,title,(select id from ".$this->tbname." where id > ".$id." order by id asc limit 1) as lastid,(select id from ".$this->tbname." where id < ".$id." order by id desc limit 1) as nextid from ".$this->tbname." where id=".$id." order by id";
		$result = $this->dosql('_select');
		return $result[0];
	}
}

?>